package edu.nf.bs.controller;

import com.github.pagehelper.PageInfo;
import edu.nf.bs.dao.LogisticsMapper;
import edu.nf.bs.dao.OrderInfoMapper;
import edu.nf.bs.dao.StationsMapper;
import edu.nf.bs.dao.impl.*;
import edu.nf.bs.entity.ExpressInfo;
import edu.nf.bs.entity.Logistics;
import edu.nf.bs.entity.Stations;
import edu.nf.bs.entity.Transfer;
import edu.nf.bs.service.LogisticsService;
import edu.nf.bs.service.impl.*;
import edu.nf.bs.util.Dates;
import edu.nf.bs.util.UUIDUtil;
import edu.nf.bs.vo.ResultVO;
import org.mvc.web.annotation.RequestMapping;
import org.mvc.web.servlet.View;
import org.mvc.web.view.JsonView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LogisticsContraller extends BaseContraller{
    @RequestMapping("/logistics/queryLogistics")
    public View listLogistics(String logistics,Integer page,Integer limit){
        LogisticsMapper mapper = new LogisticsMapperImpl();
        LogisticsServiceImpl service=new LogisticsServiceImpl();
        service.setLogisticsMapper(mapper);
        PageInfo<Logistics> logisticsPageInfo = service.queryLogistics(logistics, page, limit);
        ResultVO<List<Logistics>> logisticsPage = page(logisticsPageInfo);
        return new JsonView(logisticsPage);
    }
    @RequestMapping("/logistics/addLogistics")
    public View addLogistics(String expressId){
        List<Transfer> list=new ArrayList<>();
        ExpressInfoMapperImpl expressInfoMapper = new ExpressInfoMapperImpl();
        ExpressServiceImpl expressService = new ExpressServiceImpl();
        expressService.setExpressInfoMapper(expressInfoMapper);
        ExpressInfo expressInfo = expressService.queryExpressId(expressId);

        LogisticsMapper mapper = new LogisticsMapperImpl();
        LogisticsServiceImpl service=new LogisticsServiceImpl();
        service.setLogisticsMapper(mapper);
        //随机生成快递编号
        Long logisticsId =UUIDUtil.randomNum();
        //将快递编号转换成String类型
        String expId = Long.toString(logisticsId);
        Logistics logistics = new Logistics(expId, expressInfo.getUserAddress().substring(0,2), 1, expressId);
        service.addLogistics(logistics);
        OrderInfoMapper orderInfoMapper=new OrderInfoMapperImpl();
        OrderInfoServiceImpl orderInfoService = new OrderInfoServiceImpl();
        orderInfoService.setOrderInfoMapper(orderInfoMapper);
        orderInfoService.updateLogistics(expressId,expId);
        TransferMapperImpl transferMapper = new TransferMapperImpl();
        TransferServiceImpl transferService = new TransferServiceImpl();
        transferService.setMapper(transferMapper);
        if(expressInfo.getUserAddress().substring(0,2).equals("广东")){
            Transfer transfer=new Transfer(Long.toString(UUIDUtil.randomNum()),"广东珠海",expressInfo.getUserAddress(),expressInfo.getUserAddress(),0,Dates.getDate("2021-07-14 15:21:32"),Long.toString(logisticsId));
            Transfer transfer1=new Transfer(Long.toString(UUIDUtil.randomNum()),"广东珠海","广东珠海转运中心","广东珠海转运中心",1,Dates.getDate("2021-07-15 16:02:32"),Long.toString(logisticsId));
            Transfer transfer2=new Transfer(Long.toString(UUIDUtil.randomNum()),"广东广州","广东广州转运中心","广东广州转运中心",1,Dates.getDate("2021-07-16 20:21:32"),Long.toString(logisticsId));
            Transfer transfer3=new Transfer(Long.toString(UUIDUtil.randomNum()),"江西赣州","江西赣州运转中心","江西赣州运转中心",1,Dates.getDate("2021-07-17 06:21:32"),Long.toString(logisticsId));
            Transfer transfer4=new Transfer(Long.toString(UUIDUtil.randomNum()),"江西赣州","江西赣州安远公司","江西赣州安远公司",1,Dates.getDate("2021-07-18 10:33:32"),Long.toString(logisticsId));
            Transfer transfer5=new Transfer(Long.toString(UUIDUtil.randomNum()),"江西赣州",expressInfo.getIncomingAddress(),expressInfo.getIncomingAddress(),1,Dates.getDate("2021-07-19 19:02:32"),Long.toString(logisticsId));

            list.add(transfer);
            list.add(transfer1);
            list.add(transfer2);
            list.add(transfer3);
            list.add(transfer4);
            list.add(transfer5);
            for (Transfer s : list) {
                transferService.addStations(s);
            }

        }else if(expressInfo.getUserAddress().substring(0,2).equals("江西")){
            Transfer transfer=new Transfer(Long.toString(UUIDUtil.randomNum()),"江西赣州",expressInfo.getUserAddress(),expressInfo.getUserAddress(),0,Dates.getDate("2021-07-14 15:21:32"),Long.toString(logisticsId));
            Transfer transfer1=new Transfer(Long.toString(UUIDUtil.randomNum()),"湖南衡阳","湖南衡阳转运中心","湖南衡阳转运中心",1,Dates.getDate("2021-07-15 16:02:32"),Long.toString(logisticsId));
            Transfer transfer2=new Transfer(Long.toString(UUIDUtil.randomNum()),"湖南怀化","湖南怀化转运中心","湖南怀化转运中心",1,Dates.getDate("2021-07-16 20:21:32"),Long.toString(logisticsId));
            Transfer transfer3=new Transfer(Long.toString(UUIDUtil.randomNum()),"重庆市","重庆运转中心","重庆运转中心",1,Dates.getDate("2021-07-17 06:21:32"),Long.toString(logisticsId));
            Transfer transfer4=new Transfer(Long.toString(UUIDUtil.randomNum()),"四川成都","四川成都新都区","四川成都新都区",1,Dates.getDate("2021-07-18 10:33:32"),Long.toString(logisticsId));
            Transfer transfer5=new Transfer(Long.toString(UUIDUtil.randomNum()),"四川成都",expressInfo.getIncomingAddress(),expressInfo.getIncomingAddress(),1,Dates.getDate("2021-07-19 19:02:32"),Long.toString(logisticsId));

            list.add(transfer);
            list.add(transfer1);
            list.add(transfer2);
            list.add(transfer3);
            list.add(transfer4);
            list.add(transfer5);
            for (Transfer s : list) {
                transferService.addStations(s);
            }
        }else{
            Transfer transfer=new Transfer(Long.toString(UUIDUtil.randomNum()),"海南海口",expressInfo.getUserAddress(),expressInfo.getUserAddress(),0,Dates.getDate("2021-07-14 15:21:32"),Long.toString(logisticsId));
            Transfer transfer1=new Transfer(Long.toString(UUIDUtil.randomNum()),"广东湛江","广东湛江转运中心","广东湛江转运中心",1,Dates.getDate("2021-07-15 16:02:32"),Long.toString(logisticsId));
            Transfer transfer2=new Transfer(Long.toString(UUIDUtil.randomNum()),"湖南长沙","湖南长沙转运中心","湖南长沙转运中心",1,Dates.getDate("2021-07-16 20:21:32"),Long.toString(logisticsId));
            Transfer transfer3=new Transfer(Long.toString(UUIDUtil.randomNum()),"河北武汉","河北武汉运转中心","河北武汉运转中心",1,Dates.getDate("2021-07-17 06:21:32"),Long.toString(logisticsId));
            Transfer transfer4=new Transfer(Long.toString(UUIDUtil.randomNum()),"山东济南","山东济南市中区","山东济南市中区",1,Dates.getDate("2021-07-18 10:33:32"),Long.toString(logisticsId));
            Transfer transfer5=new Transfer(Long.toString(UUIDUtil.randomNum()),"山东济南",expressInfo.getIncomingAddress(),expressInfo.getIncomingAddress(),1,Dates.getDate("2021-07-19 19:02:32"),Long.toString(logisticsId));

            list.add(transfer);
            list.add(transfer1);
            list.add(transfer2);
            list.add(transfer3);
            list.add(transfer4);
            list.add(transfer5);
            for (Transfer s : list) {
                transferService.addStations(s);
            }
        }


        ResultVO success = success();

        return new JsonView(success);
    }
}
